package kr.co.mustgo.controller.qna;

import java.util.Map;

import kr.co.mustgo.domain.qna.QnABoard;
import kr.co.mustgo.model.service.qna.QnABoardService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/qna/")
public class QnABoardController {

	@Autowired
	private QnABoardService qnaBoardService;
	
	@RequestMapping("write_form.do")
	public String writeForm() {
		return "tiles3/qna/write_form.jsp";
	}
	
	@RequestMapping("writeContent.do")
	public ModelAndView writeContent(QnABoard qnaBoard){
		ModelAndView mv = null;
			qnaBoardService.writeContent(qnaBoard);
			mv = new ModelAndView("redirect:getContent.do?code="+qnaBoard.getCode());
		return mv;
	}
	
	@RequestMapping("getContent.do")
	public String getContent(int code, ModelMap map){
		String url = null;
		try{
			//2. Business Logic 요청 - Model
			QnABoard qnaBoard = qnaBoardService.getContentByCode(code);
			map.addAttribute("qnaBoard", qnaBoard);
			url = "tiles3/qna/show_content.jsp"; 
		}catch(Exception e){
			e.printStackTrace();
			map.addAttribute("error_message", "글 조회도중 오류 발생 : "+e.getMessage());
			url = "tiles2/error/error.jsp";
		}	
		return url;
	}
	
	@RequestMapping("modify_form.do")
	public String modifyForm(int code, ModelMap map) {
		String url = null;
		try{
			//2. B.L 처리
			QnABoard qnaBoard = qnaBoardService.getContentByNoForForm(code);
			url = "tiles3/qna/modify_form.jsp";
			map.addAttribute("qnaBoard", qnaBoard);
		}catch(Exception e){
			e.printStackTrace();
			url = "tiles2/error/error.jsp";
			map.addAttribute("error_message", "수정할 글 조회도중 오류발생 : "+e.getMessage());
		}
		return url;
	}
	
	@RequestMapping("modifyContent.do")
	public ModelAndView modifyContent(QnABoard qnaBoard, int page) {
		System.out.println("AAA");
		System.out.println(qnaBoard.getCategory());
		ModelAndView mv = null;
		try {			
			//2. B.L 요청
			qnaBoardService.modifyContent(qnaBoard);
			mv = new ModelAndView("redirect:getContent.do?code="+qnaBoard.getCode()+"&page="+page);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("tiles2/error/error.jsp","error_message", "수정처리중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("list.do")
	public String list(@RequestParam(defaultValue="1")String page, ModelMap model) throws Exception{
		
		String url = null;
		//2. Business Logic 호출 ->model		
			//page=  로 넘어온 경우(위 @RequestParam 설정은 page 요청파라미터 이름으로 넘어온 것이 없는 경우이다.
		int pageNo = 1;
		try{
			pageNo = Integer.parseInt(page);
		}catch(Exception e){}
		Map map = qnaBoardService.getQnABoardList(pageNo);
		model.addAllAttributes(map);
		url = "tiles3/qna/list.jsp";
		return url;
	}
	
	@RequestMapping("deleteContent.do")
	public String deleteContent(int code, int page, ModelMap map){
		String url = null;
		try {
			//2. BL
			qnaBoardService.deleteContentByCode(code);
			url = "redirect:list.do?page="+page;
		}catch (Exception e) {
			e.printStackTrace();
			url = "tiles2/error/error.jsp";
			map.addAttribute("error_message", "삭제도중 오류 발생 : "+e.getMessage());
		}
		return url;
	}
	
	@RequestMapping("replyForm.do")
	public String replyForm(int code, ModelMap map){
		//1. 요청 파라미터 조회 - no
		QnABoard qnaBoard = qnaBoardService.getContentByNoForForm(code);
		map.addAttribute("qnaBoard", qnaBoard);
		return "tiles3/qna/reply_form.jsp";
	}
	
	@RequestMapping("replyContent.do")
	public ModelAndView replyContent(QnABoard qnaBoard,  @RequestParam(required=false, defaultValue="1") String page){
		ModelAndView mv = null;
		if(page.equals("")){
			page="1";
		}
		qnaBoardService.replyContent(qnaBoard);
		mv = new ModelAndView("redirect:getContent.do?code="+qnaBoard.getCode()+"&page="+page);
		return mv;
	}
	

	@RequestMapping("categoryList.do")
	public String categoryList(@RequestParam(defaultValue="1")String page, ModelMap model, String category) throws Exception{
		
		
		String url = null;
		//2. Business Logic 호출 ->model		
			//page=  로 넘어온 경우(위 @RequestParam 설정은 page 요청파라미터 이름으로 넘어온 것이 없는 경우이다.
		int pageNo=1;
		try{
			pageNo = Integer.parseInt(page);
		}catch(Exception e){}
		Map map = qnaBoardService.getQnABoardListCategory(pageNo, category);
		model.addAllAttributes(map);
		url = "tiles3/qna/categoryList.jsp";
		return url;
	}
	
	@RequestMapping("search.do")
	public String search(@RequestParam(defaultValue="1")String page, ModelMap model, String search, String keyword) throws Exception{
		
		String url = null;
		//2. Business Logic 호출 ->model		
			//page=  로 넘어온 경우(위 @RequestParam 설정은 page 요청파라미터 이름으로 넘어온 것이 없는 경우이다.
		int pageNo=1;
		try{
			pageNo = Integer.parseInt(page);
		}catch(Exception e){}
		
		Map map = qnaBoardService.getQnABoardSearch(pageNo, search, keyword);
		model.addAllAttributes(map);
		url = "tiles3/qna/searchList.jsp";
		return url;
	}
	
}
